<?php


namespace app\Activity\controller;


use app\activity\model\Activity;
use app\AdminRest;
use app\activity\model\ActivityClassify as ActivityClassifyModel;
use app\activity\validate\ActivityClassifyValidate;



class AdminActivityClassify extends AdminRest
{
    public function listActivityClassify()
    {
        $param = $this->_param;
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter['uniacid'] = $this->_uniacid;
        $filter = [
            ['uniacid', '=', $this->_uniacid],
            ['status', '>', -1],
        ];
        $model = new ActivityClassifyModel();
        $page_config['total'] = $model->countByFilter($filter);
        $rsts = $model->getListByFilter($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $result['data'] = $rsts;
        return $this->success($result);


    }

    public function listAllForSelect()
    {
        $filter['uniacid'] = $this->_uniacid;
        $filter = [
            ['uniacid', '=', $this->_uniacid],
            ['status', '=', 1],

        ];

        $model = new ActivityClassifyModel();
        $rsts = $model->where($filter)->field('id,title')
            ->order('top', 'desc')
            ->select()->toArray();

        return $this->success($rsts);

    }

    public function createActivityClassify()
    {
        $validate = new ActivityClassifyValidate();
        $check = $validate->scene('create')->check($this->_input);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }
        $model = new ActivityClassifyModel();
        $input = $this->_input;

        if (empty(trim($input['title'])))
        {

            return $this->error('标题必填');
        }


        if ($model->where(['title' => $input['title']])->count())
        if ($model->where([['title', '=', $input['title']], ['uniacid', '=', $this->_uniacid],['status','>',-1]])->count())
        {
            return $this->error('this classify is exists');
        }

        $input['uniacid'] = $this->_uniacid;
        $res = $model->createOne($input);
        return $this->success($res);
    }

    public function updateActivityClassify()
    {
        $validate = new ActivityClassifyValidate();
        $check = $validate->scene('update')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }

        $input = $this->_input;
        $model = new ActivityClassifyModel();
        $classify_id = $this->_param['id'];

        if (empty(trim($input['title'])))
        {

            return $this->error('标题必填');
        }

        if ($model->where([
            ['id', '<>', $classify_id],
            ['title', '=', $input['title']],
            ['uniacid', '=', $this->_uniacid]
            ,['status','>',-1]
        ])->count())
        {
            return $this->error('this classify is exists');
        }


        unset($input['status']);
        $data = $model->updateByfilter(['id' => $classify_id], $input);
        return $this->success($data);
    }

    public function getActivityClassify()
    {
        $validate = new ActivityClassifyValidate();
        $check = $validate->scene('get')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }
        $classify_id = $this->_param['id'];
        $model = new ActivityClassifyModel();
        $data = $model->getOne(['id' => $classify_id]);
        return $this->success($data);
    }

    public function changeStatus()
    {
        $validate = new ActivityClassifyValidate();
        $check = $validate->scene('updateStatus')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }

        $classify_id = $this->_param['id'];
        $input = $this->_input;

        if ($input['status'] != 1 && Activity::where([['classify_id', '=', $classify_id], ['status', '=', 1] ])->count())
        {
            return $this->error('this classify has activity');
        }

        $model = new ActivityClassifyModel();
        $rst = $model->updateByfilter(['id' => $classify_id], ['status' => $input['status']]);
        return $this->success($rst);
    }




}